# -*- coding: utf-8 -*-

'''
    <img class="BDE_Image" src="https://imgsa.baidu.com/forum/w%3D580/sign=269396684d4a20a4311e3ccfa0539847/0aa95edf8db1cb132cd1f269df54564e92584b15.jpg" pic_ext="jpeg" width="510" height="765">
    :param info:
    :return:
    '''

import re
import urllib

def get_content(url):
    html = urllib.urlopen(url)
    content = html.read()
    html.close()
    return content

'''

def get_images(info):

    regex = r'class ="BDE_Image" src = "(.+?\.jpg)"'
    pat = re.compile(regex)

    images_code = re.findall(pat,info)
    i=0
    for image_url in images_code:
        print image_url

        urllib.urlretrieve(image_url,'/Users/wangjunjie/Documents/照片/%s.jpg' %i)
        i += i

info = get_content('http://tieba.baidu.com/p/2772656630')
get_images(info)
'''

from bs4 import BeautifulSoup

def get_images(info):

    soup = BeautifulSoup(info,"html.parser")
    all_img = soup.find_all("a",class_="item-cover j-pl-photoitem-imgctn")

    x = 0
    for image in all_img:
        image_name = '%s.jpg' % x
        print image_name
        print image['src']
        urllib.urlretrieve(image['src'], "/Users/wangjunjie/Documents/照片/壁纸/"+image_name)
        x += 1
#info = get_content('http://tieba.baidu.com/p/2772656630')
#print get_images(info)

import urllib2
url1 = "https://user.qzone.qq.com/776764652?ptlang=2052&_t_=0.36146413851712356"
req = urllib2.Request(url1)
req.add_header("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36")
req.add_header("GET",url1)
req.add_header("authority","user.qzone.qq.com") #主机
req.add_header("Referer","http://qzs.qzone.qq.com/qzone/v5/loginsucc.html?para=izone&specifyurl=http%3A%2F%2Fuser.qzone.qq.com%2F776764652%3Fptlang%3D2052")#从哪里访问来的，一般说是这个网页只能从它的网页内部的网页访问
req.add_header("scheme","https")
req.add_header("avail-dictionary","XprLfaXG")
html = urllib2.urlopen(req)
info = html.read()
print info
html.close()

print get_images(info)